<?php

namespace G659\Controller;

use Think\Controller;

class PublicController extends Controller {
    
//是否登录
    public function islogin(){
        if(session('user.id')>0){
            $this->success('是');
        }else{
            $this->error('否');
        }
    }
   
    //$admin=''//sb_123123123jjkkjk
    public function login($admin='',$password='') {
        if (IS_POST) {
            $map['admin']=$admin;
            $map['password']=$password;
            $user = M('g')->where($map)->field('id')->find();
            if ($user) {
                session('user',$user);
                $data=['id_g'=>$user['id'],'id_c'=>38,'sku'=>''];
                M('u')->add($data, [], 'sku');	
                $this->success('登录成功！', U('Index/index'));
            } else {
                $this->error('用户名或密码错误!');
            }
        }else{
             $this->display();
        }
    }

    public function logout() {
        session(null);
        $this->redirect('Public/login');
    }
    

    public function reg($admin,$password) {
        if (IS_POST) {
            $map['admin']=$admin;
            $user = M('g')->where($map)->field('id')->find();
            if($user){
                $this->error('已被注册！');
            }else {
                $map['password']=$password;
                //$map['id']=date('Y-m-d H:i:s');
                $user['id'] =  M('g')->add($map);
                session('user',$user);
                M('u')->add(['id_g'=>$user['id'],'id_c'=>38,'sku'=>'']);
                $this->success('注册成功！自动登入中！', U('Index/index'));
            } 
        }
    }
    
}
